package com.wz.jiangsu.main.huiwenshu;



public class TestCal {
    public static void main(String[] args) {
        int[] arr = {1,4,3,3,5,6,17,844,-9,2};

        arr = starNiZ(arr);
        System.out.print("开始逆转：");
        for (int i : arr) {
            System.out.print(i + "\t");
        }
        arr = getBubble(arr);
        System.out.print("冒泡排序：");
        for (int i : arr) {
            System.out.print(i + "\t");
        }
        System.out.println();
        System.out.print("二分查找:");

        int index = startErFen(arr,10);
        System.out.print(index);

    }

    private static int[] starNiZ(int[] arr) {
        int length = arr.length;
        for (int i = 0; i < length / 2 ; i++) {
            int temp = arr[i];
            arr[i] = arr[length - i - 1];
            arr[length - i - 1] = temp;
        }
        return arr;
    }

    private static int startErFen(int[] arr, int target) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (arr[mid] == target) {
                return mid;
            }else if (arr[mid] > target) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return -1;
    }

    private static int[] getBubble(int[] arr) {
        int length = arr.length;
        for (int i = 0; i < length -1 ; i++) {
            boolean flag = false;
            for (int j = 0; j < length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    flag = true;
                }
            }
            if (!flag) {
                break;
            }

        }
        return arr;
    }
}
